package com.mccken.practice.top100.Top_40;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import com.mccken.practice.top100.TreeNode;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2024-04-02 12:21
 **/
public class LevelOrder_102 {

	public static void main(String[] args) {
		LevelOrder_102 test = new LevelOrder_102();

	}

	public List<List<Integer>> levelOrder(TreeNode root) {

		List<List<Integer>> ansList = new ArrayList<>();
		if (root == null) {
			return ansList;
		}
		Queue<TreeNode> queue = new LinkedList<>();
		queue.offer(root);
		while (!queue.isEmpty()) {
			List<Integer> level = new ArrayList<>();
			int currentLevelSize = queue.size();
			for (int i = 0; i < currentLevelSize; i++) {
				TreeNode node = queue.poll();
				level.add(node.val);

				if (node.left != null) {
					queue.offer(node.left);
				}
				if (node.right != null) {
					queue.offer(node.right);
				}
			}
			ansList.add(level);
		}
		return ansList;
	}
}
